//
//  LZMyHeadButton.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/26.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyHeadButton.h"

#import "Masonry.h"

#import "LZUserModel.h"

@implementation LZMyHeadButton

- (instancetype)initWithButtonType:(LZ_MY_HEAD_BUTTON_TYPE)type {
    
    self = [super init];
    if (self) {
        
        self.type = type;
        
        [self setup];
        
        [self addAutoLayout];
        
    }
    return self;
}

- (void)setup {
    
    [self addSubview:self.iconimageView];
    [self addSubview:self.subtitleLabel];
    [self addSubview:self.numberButton];
    
    if (self.type == LZ_MY_HEAD_BUTTON_TYPE_COUPONS) {
        [self.iconimageView setImage:[UIImage imageNamed:@"lz_mycoupons_icon"]];
        self.subtitleLabel.text     = LZLocalizedString(@"mine_coupons", nil);
    }
    else if (self.type == LZ_MY_HEAD_BUTTON_TYPE_TOBEREVIEW) {
        [self.iconimageView setImage:[UIImage imageNamed:@"lz_mytobereview_icon"]];
        self.subtitleLabel.text     = LZLocalizedString(@"mine_to_be_reviewed", nil);
    }
    else {
        [self.iconimageView setImage:[UIImage imageNamed:@"lz_my_review_icon"]];
        self.subtitleLabel.text     = LZLocalizedString(@"mine_my_reviews", @"");//@"My reviews";
        self.numberButton.hidden    = YES;
    }
}

- (void)addAutoLayout {

    WS(weakSelf);
    
    [self.iconimageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.centerX.mas_equalTo(weakSelf.centerX);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    [self.subtitleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-20);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.top.mas_equalTo(weakSelf.iconimageView.mas_bottom).with.offset(10);
    }];
    
    [self.numberButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.iconimageView.mas_top).with.offset(-8);
        make.leading.mas_equalTo(weakSelf.iconimageView.trailing).with.offset(-6);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
}

- (void)setNumberButtonHide:(BOOL)hide {
    self.numberButton.hidden = hide;
}

- (void)setNumberButtonNumTitle:(NSInteger)num {
    if (num>0) {
        self.numberButton.hidden = NO;
        [self.numberButton setTitle:[NSString stringWithFormat:@"%ld",num] forState:UIControlStateNormal];
    }
    else {
        self.numberButton.hidden = YES;
    }
}

- (UIImageView *)iconimageView {
    
    if (_iconimageView == nil) {
        _iconimageView = [[UIImageView alloc] init];
    }
    
    return _iconimageView;
}

- (UILabel *)subtitleLabel {
    
    if (_subtitleLabel == nil) {
        CGFloat scale = SCREEN_WIDTH/375.0;
        _subtitleLabel = [[UILabel alloc] init];
        _subtitleLabel.textColor        = UIColorFromRGB(0x666666, 1);
        _subtitleLabel.textAlignment    = NSTextAlignmentCenter;
        _subtitleLabel.font             = [UIFont systemFontOfSize:14*scale];
    }
    
    return _subtitleLabel;
}

- (UIButton *)numberButton {
    
    if (_numberButton == nil) {
        _numberButton = [[UIButton alloc] init];
        _numberButton.hidden                = YES;
        _numberButton.layer.masksToBounds   = YES;
        _numberButton.layer.cornerRadius    = 9.0;
        _numberButton.titleLabel.font       = [UIFont systemFontOfSize:10];
        [_numberButton setBackgroundImage:[UIImage imageNamed:@"lz_mybutton_hintimage"] forState:UIControlStateNormal];
        [_numberButton setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
    }
    
    return _numberButton;
}

@end
